﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FazaDemo.BL.InterfaceDomain;
using FazaDemo.BL.SubDomains;
using NUnit.Framework;

namespace FazaDemo.BL.Services_
{
    [TestFixture]
    public class NavElementsDistincterTests
    {

        private NavElementsDistincter _distincter;

        private IList<NavCondElement> _condElems1;

        private IList<NavCondElement> _condElems2;

        private IList<NavCondElement> _condElemsDesc;

        [SetUp]
        public void TestInit()
        {
            _distincter = new NavElementsDistincter();
            _condElems1 = new List<NavCondElement>
            {
                    new NavCondElement {ElemText = "а (спр)", FieldDropType = 'N', IsShowQuery = true}, 
                    new NavCondElement {ElemText = "а", FieldDropType = 'C', IsShowQuery = false}, 
                    new NavCondElement {FieldDropType = 'C', IsShowQuery = true}
                };
            _condElems2 = new List<NavCondElement>
            {
                new NavCondElement {IsShowQuery = true}, 
                new NavCondElement {IsShowQuery = true}, 
                new NavCondElement {ElemText = "б (спр)", FieldDropType = 'F', IsShowQuery = true},
                new NavCondElement {ElemText = "в (спр)", FieldDropType = 'N', IsShowQuery = true}, 
                new NavCondElement {ElemText = "г (спр)", FieldDropType = 'F', IsShowQuery = true}, 
                new NavCondElement {ElemText = "б", FieldDropType = 'C', IsShowQuery = false}, 
                new NavCondElement {ElemText = "в", FieldDropType = 'C', IsShowQuery = false}, 
                new NavCondElement {ElemText = "г", FieldDropType = 'C', IsShowQuery = false}, 
                new NavCondElement {IsShowQuery = true}
            };
            _condElemsDesc = new List<NavCondElement>
            {
                new NavCondElement{IsShowQuery = true}, 
                new NavCondElement{ElemText = "д", FieldDropType = 'C', IsShowQuery = false}, 
                new NavCondElement{ElemText = "е", FieldDropType = 'C', IsShowQuery = false}, 
                new NavCondElement{ElemText = "д (спр)", FieldDropType = 'N', IsShowQuery = true},
                new NavCondElement{ElemText = "е (спр)", FieldDropType = 'F', IsShowQuery = true}
            };
        }

        [Test]
        [TestCaseSource("GetConditions")]
        public Tuple<int, bool> Resolve_VarCondElems_ReturnResultElemsCountAndNoShowQueryChecking(IList<NavCondElement> elements)
        {
            try
            {
                IList<NavCondElement> resultElems = _distincter.Resolve(elements);
                return new Tuple<int, bool>(resultElems.Count, resultElems.Any(e => !e.IsShowQuery));
            }
            catch (Exception)
            {
                return null;
            }
        }

        [Test]
        [TestCaseSource("GetDropTypeConditions")]
        public char? Resolve_varCondElems_ReturnRandomFieldDropTypeValue(IList<NavCondElement> elements, int index)
        {
            try
            {
                IList<NavCondElement> resultElems = _distincter.Resolve(elements);
                return resultElems[index].FieldDropType;
            }
            catch (Exception)
            {
                return default(char);
            }
        }

        private IEnumerable<TestCaseData> GetConditions()
        {
            TestInit();
            yield return new TestCaseData(_condElems1).Returns(new Tuple<int, bool>(2, false));
            yield return new TestCaseData(_condElems2).Returns(new Tuple<int, bool>(6, false));
            yield return new TestCaseData(_condElemsDesc).Returns(new Tuple<int, bool>(3, false));
        }

        private IEnumerable<TestCaseData> GetDropTypeConditions()
        {
            TestInit();
            yield return new TestCaseData(_condElems1, 0).Returns('N');
            yield return new TestCaseData(_condElems2, 4).Returns('F');
            yield return new TestCaseData(_condElemsDesc, 2).Returns('F');
        }

    }
}
